﻿(function () {
    'use strict';
    var myApp = angular.module('app');
    myApp.directive("loader", ['$rootScope',
        function ($rootScope) {
            return function ($scope, element, attrs) {
                $scope.$on("loader_show", function () {
                    if (attrs.elemToDisable) {
                        angular.element(attrs.elemToDisable).prop('disabled', true);
                    }
                    return element.show();
                });
                return $scope.$on("loader_hide", function() {
                    if (attrs.elemToDisable) {
                        angular.element(attrs.elemToDisable).prop('disabled', false);
                    }
                    return element.hide();
                });
            };
        }
    ]);
    myApp.directive("inputFileUploader", ['$timeout',
        function ($timeout) {
            return {
                link: function (scope, elem) {
                    $timeout(function() {
                        elem.ace_file_input({
                            no_file: 'No File ...',
                            btn_choose: 'Choose',
                            btn_change: 'Change',
                            droppable: false,
                            onchange: null,
                            thumbnail: false //| true | large
                            //whitelist:'gif|png|jpg|jpeg'
                            //blacklist:'exe|php'
                            //onchange:''
                            //
                        });
                    });
                }
            };
        }
    ]);

    myApp.directive("fileupload", ['primeData', function (primeData) {
        return {
            restrict: "A",
            scope: {
                done: "&",
                progress: "&",
                fail: "&",
                uploadurl: "=",
                customdata: "&"
            },
            link: function (scope, elem, attrs) {
                var uploadOptions;
                uploadOptions = {
                    url: scope.uploadurl,
                    dataType: "json",
                    beforeSend: function (xhr, data) {
                        xhr.setRequestHeader('Authorization', 'Bearer ' + primeData.getUser().Token);
                    }
                };
                if (scope.done) {
                    uploadOptions.done = function (e, data) {
                        return scope.$apply(function () {
                            return scope.done({
                                e: e,
                                data: data
                            });
                        });
                    };
                }
                if (scope.fail) {
                    uploadOptions.fail = function (e, data) {
                        return scope.$apply(function () {
                            return scope.fail({
                                e: e,
                                data: data
                            });
                        });
                    };
                }
                if (scope.progress) {
                    uploadOptions.progress = function (e, data) {
                        return scope.$apply(function () {
                            return scope.progress({
                                e: e,
                                data: data
                            });
                        });
                    };
                }
                return elem.fileupload(uploadOptions).bind("fileuploadsubmit", function (e, data) {
                    return data.formData = {
                        params: JSON.stringify(scope.customdata())
                    };
                });
            }
        };
    }]);

})();